package com.yxt.ui;

import java.util.ArrayList;
import java.util.List;
import com.yxt.common.sys.CommConstant;
import com.yxt.common.ui.ActivityStackControlUtil;
import com.yxt.common.ui.NormalTableAdapter;
import com.yxt.common.ui.NormalTableAdapter.TableCell;
import com.yxt.common.ui.NormalTableAdapter.TableRow;
import com.yxt.model.Favirote;
import com.yxt.service.FavoriteService;
import com.yxt.service.StationService;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

/**
 * 热点水情
 * @author Crazyman
 */
public class HotWaterListActivity extends Activity {
	private int cols = 2;
	private int itemHeight = 70;
	private int color = Color.WHITE;
	private StationService service = new StationService(HotWaterListActivity.this);

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.hotwater_list); // 软件activity的布局
		ActivityStackControlUtil.add(this);
		initComponent();
	}
	
	private void initComponent() {
		Button btnBackHome = (Button) findViewById(R.id.btnBackHome);
		btnBackHome.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		fillStationList();
	}
	
	private void fillStationList() {
		List<TableRow> table = new ArrayList<TableRow>();
		int[] width = { 250, 50 };
		FavoriteService service = new FavoriteService(HotWaterListActivity.this);
		List<Favirote> list = service.findFaviroteListByType(String.valueOf(CommConstant.Station.RAIN_LOCATION));
		for (int i = 0; i < list.size(); i++) {
			Favirote favirote = list.get(i);
			TableCell[] cells = new TableCell[cols];
			String type = favirote.getType();
			String locationtype = CommConstant.STATIONTYPE_MAP.get(type);
			cells[0] = new TableCell(locationtype + " ■ " + favirote.getName() + "@" + favirote.getAddr(), width[0],itemHeight, color, TableCell.DEFINE,new TextItemOnClickListener(favirote));
			cells[1] = new TableCell(R.drawable.expand, width[1], itemHeight, color, TableCell.IMAGE, new ImageItemOnClickListener(favirote));
			table.add(new TableRow(cells));
		}
		NormalTableAdapter tableAdapter = new NormalTableAdapter(this, table);
		ListView lstWaterStation = (ListView) findViewById(R.id.lstStationList);
		lstWaterStation.setAdapter(tableAdapter);
	}
	
	private class TextItemOnClickListener implements OnClickListener {
		private Favirote favirote;

		public TextItemOnClickListener(Favirote favirote) {
			this.favirote = favirote;
		}
		
		public TextItemOnClickListener() {
			
		}

		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra("code", favirote.getCode());
			intent.putExtra("name", favirote.getName());
			int type = Integer.parseInt(favirote.getType());
			switch(type){
				case CommConstant.Station.RAIN_LOCATION:
					intent.setClass(getApplicationContext(),RainStationDetailActivity.class);
					break;
				case CommConstant.Station.WATER_LOCATION:
					intent.setClass(getApplicationContext(),WaterStationDetailActivity.class);
					break;
				case CommConstant.Station.SOIL_LOCATION:
					intent.setClass(getApplicationContext(),SoilStationDetailActivity.class);
					break;
				case CommConstant.Station.SALT_LOCATION:
					intent.setClass(getApplicationContext(),SaltStationDetailActivity.class);
					break;
			}			
			startActivity(intent);
		}

	}
	
	private class ImageItemOnClickListener implements OnClickListener {
		private Favirote favirote;

		public ImageItemOnClickListener(Favirote favirote) {
			this.favirote = favirote;
		}

		public void onClick(View v) {
			Builder builder = new AlertDialog.Builder(HotWaterListActivity.this);
			builder.setTitle("测站删除")
			.setMessage("是否确定要删除此测站").setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialoginterface,int i) {
							FavoriteService service = new FavoriteService(HotWaterListActivity.this);
							service.delete(favirote.getCode());
							fillStationList();
						}
					}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialoginterface,int i) {
							
						}
					}).show();
		}
	}

	protected void onDestroy() {
		super.onDestroy();
		service.closeDB();
        ActivityStackControlUtil.remove(this);
	}
}